//Copyright 2008 by Wes Corp for Trafficman  All rights reserved.abstract 

package org.trafficman.trafficman_main;

// import com.google.android.samples.R;

import android.app.Activity; 
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import android.widget.RadioGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.LinearLayout;

public class TrafficmanFilesLocationNotepad extends Activity implements
		RadioGroup.OnCheckedChangeListener, View.OnClickListener {

	private TextView mChoice;
	private RadioGroup mRadioGroup;
	private String rb1_text;
	private String rb2_text;
	private String datetime_record_updated;
	private String datetime_record_created;
	private String ext_stor_state;
	private CharSequence str_Storage_Device;
	private int android_device_id;
	private int sd_device_id;
	private int int_intent_active;

	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		Window w = getWindow();
		w.requestFeature(Window.FEATURE_LEFT_ICON);

		setContentView(R.layout.file_locations);
		setContentView(R.layout.radio_group_1);
		mRadioGroup = (RadioGroup) findViewById(R.id.menu);

		LinearLayout tmpLayout = (LinearLayout) findViewById(R.id.linearlayout_001);
		TrafficmanConstants.SCREEN_VIEWWIDTH = this.getResources().getDisplayMetrics().widthPixels;
		TrafficmanConstants.SCREEN_VIEWHEIGHT = this.getResources().getDisplayMetrics().heightPixels;
		get_JobView_Screen(); // set JobView2 background
		int resID = getResources().getIdentifier(TrafficmanConstants.SCREEN_NAME, null,
				null);
		tmpLayout.setBackground(resID);

		RadioButton rb_1 = (RadioButton) findViewById(R.id.radio1);
		rb1_text = getString(R.string.devices_local);
		rb_1.setText(rb1_text);
		android_device_id = rb_1.getId();

		RadioButton rb_2 = (RadioButton) findViewById(R.id.radio2);
		rb2_text = getString(R.string.devices_sdcard);
		sd_device_id = rb_2.getId();

		// test listening to checked change events
		String selection = getString(R.string.radio_group_1_selection);
		mRadioGroup.setOnCheckedChangeListener(this);
		mChoice = (TextView) findViewById(R.id.storage_note);

		get_Default_Active_Storage_Device();

		if (TrafficmanConstants.ACTIVE_DEVICE.equals("sdcard")) {
			ext_stor_state = android.os.Environment.getExternalStorageState();
			if (ext_stor_state.equals("mounted")) {
				rb2_text = rb2_text + " \"available\"";
				rb_2.setText(rb2_text);
				rb_1.setChecked(false);
				rb_2.setChecked(true);
				str_Storage_Device = "Current active storage device = SDCard";

			} else {
				// sdcard not available
				set_Storage_Device("android device");
				rb_1.setChecked(true);
				rb_2.setClickable(false);
				rb2_text = rb2_text + " \"not available\"";
				rb_2.setText(rb2_text);
				str_Storage_Device = "Current active storage device = Android Device";

			}
		} else {
			if (TrafficmanConstants.ACTIVE_DEVICE.equals("android device")) {

				rb_1.setChecked(true);

				// check if sdcard available
				ext_stor_state = android.os.Environment
						.getExternalStorageState();
				if (ext_stor_state.equals("mounted")) {
					rb2_text = rb2_text + " \"available\"";
					rb_2.setText(rb2_text);
					// rb_2.setChecked(false);
				} else {
					rb_2.setClickable(false);
					rb2_text = rb2_text + " \"not available\"";
					rb_2.setText(rb2_text);
				}
			} else {
				// should never be here
			}
			str_Storage_Device = "Current active storage device = Android Device";
		}

		// // test adding a radio button programmatically
		// RadioButton newRadioButton = new RadioButton(this);
		// newRadioButton.setText(R.string.radio_group_snack);
		// newRadioButton.setId(R.id.snack);
		// LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams(
		// RadioGroup.LayoutParams.WRAP_CONTENT,
		// RadioGroup.LayoutParams.WRAP_CONTENT);
		// mRadioGroup.addView(newRadioButton, 0, layoutParams);

		mChoice.setText(str_Storage_Device);

		// test clearing the selection
		Button clearButton = (Button) findViewById(R.id.clear);
		clearButton.setOnClickListener(this);
	}

	public void onCheckedChanged(RadioGroup group, int checkedId) {
		if (checkedId == android_device_id) {
			set_Storage_Device("android device");
			str_Storage_Device = "Current active storage device = Android Device";
		} else {
			if (checkedId == sd_device_id) {
				set_Storage_Device("sdcard");
				str_Storage_Device = "Current active storage device = SDCard";
			}
		}
		mChoice.setText(str_Storage_Device);
	}

	public void onClick(View v) {
		//mRadioGroup.clearCheck();
		int_intent_active = 1;
		Intent intent = new Intent();
		intent.setClass(TrafficmanFilesLocationNotepad.this, TrafficmanMainActivity.class);
		startActivity(intent);
		finish();
	}

	private void get_JobView_Screen() {
		TrafficmanScreens mScreens = new TrafficmanScreens(this);
	}

	private void get_Default_Active_Storage_Device() {
		TrafficmanFilesLocationNotesDbAdapter active_device_mDbHelper = new TrafficmanFilesLocationNotesDbAdapter(
				this);
		active_device_mDbHelper.open();
		active_device_mDbHelper.fetch_Active_Storage_Location();
		if (TrafficmanConstants.ACTIVE_DEVICE.equals("")) {

			new TrafficmanDateStamp();
			datetime_record_updated = TrafficmanConstants.DATETIME_NOW;
			TrafficmanConstants.DATETIME_NOW = "";
			datetime_record_created = datetime_record_updated;

			active_device_mDbHelper.createNote("android device", 1,
					datetime_record_created, datetime_record_updated);

			active_device_mDbHelper.createNote("sdcard", 0,
					datetime_record_created, datetime_record_updated);

			active_device_mDbHelper.fetch_Active_Storage_Location();
		}
		active_device_mDbHelper.close();
	}

	private void set_Storage_Device(String device) {

		TrafficmanFilesLocationNotesDbAdapter set_Storage_Device_mDbHelper = new TrafficmanFilesLocationNotesDbAdapter(
				this);
		set_Storage_Device_mDbHelper.open();

		Cursor device_Cursor = set_Storage_Device_mDbHelper.fetchAllNotes();
		startManagingCursor(device_Cursor);
		int record_count = device_Cursor.count();
		if (record_count > 0) {
			device_Cursor.first();
			int columnID = device_Cursor.getColumnIndex("selected_location");
			int int_Device = device_Cursor.getColumnIndex("device");
			for (int i = 0; i < record_count; ++i) {
				if (device_Cursor.getString(int_Device).equals(device)){
					device_Cursor.updateInt(columnID, 1);
				} else {
					device_Cursor.updateInt(columnID, 0);
				}
				device_Cursor.next();
			}
			device_Cursor.commitUpdates();
		}
		set_Storage_Device_mDbHelper.close();
	}

	@Override
	protected void onPause() {
		if (int_intent_active == 1) {
			// do nothing
			int_intent_active = 0;
			super.onPause();
		} else {
			super.onPause();
			Intent intent = new Intent();
			intent.setClass(TrafficmanFilesLocationNotepad.this, TrafficmanMainActivity.class);
			startActivity(intent);
			finish();
		}
	}

}
